package com.rjwh.dingdong.client.network;

import com.aishua.lib.task.AsResult;
import com.rjwh.dingdong.client.application.MyApplication;
import com.rjwh.dingdong.client.constant.LocalConstant;
import com.rjwh.dingdong.client.constant.NetConstant;
import com.rjwh.dingdong.client.util.TheradPoolUtil;

import net.tsz.afinal.http.AjaxCallBack;
import net.tsz.afinal.http.AjaxParams;

import java.util.Map;

/**
 * 请求工具类
 *
 * @author 王博扬
 * @ClassName HttpWebService
 * @Description TODO(这里用一句话描述这个类的作用)
 * @date 2015-11-19 上午11:33:59
 * @history 1.YYYY-MM-DD author: description:
 */
public class HttpWebService {
    /**
     * 请求WebServer
     *
     * @param @param businessType
     * @param @param params
     * @param @param isRefresh
     * @param @param callBack
     * @return void
     * @throws
     * @Title: getDataFromServer
     * @Description: TODO(这里用一句话描述这个方法的作用)
     * @history 1.YYYY-MM-DD author: description:
     */
    public static void getDataFromServer(final int businessType,
                                         final Map<String, String> params, final boolean isRefresh,
                                         final HttpWebServiceCallBack callBack) {
        if (!params.containsKey("platform")) {
            params.put("platform", "ANDROID");
        }
        if (!params.containsKey("version")) {
            params.put("version", MyApplication.versionCode);
        }
        if (!params.containsKey("token")) {
            params.put("token", MyApplication.spUtil
                    .getStrPreferenceByParamName(MyApplication.app, LocalConstant.SP_USER_TOKEN));
        }
        if (!params.containsKey("udid")) {
            params.put("udid", MyApplication.deviceId);
        }
        if (!params.containsKey("babyid")) {
            params.put("babyid", MyApplication.spUtil
                    .getStrPreferenceByParamName(MyApplication.app,
                            LocalConstant.SP_BABYID));
        }
        if (!params.containsKey("userid")) {
            params.put("userid", MyApplication.spUtil
                    .getStrPreferenceByParamName(MyApplication.app,
                            LocalConstant.SP_USERID));
        }
        getDataWebServer(businessType, params, isRefresh, callBack);
    }

    /**
     * 请求WebServer
     *
     * @param @param businessType
     * @param @param params
     * @param @param isRefresh
     * @param @param callBack
     * @return void
     * @throws
     * @Title: getDataWebServer
     * @Description: TODO(这里用一句话描述这个方法的作用)
     * @history 1.YYYY-MM-DD author: description:
     */
    private static void getDataWebServer(final int businessType,
                                         final Map<String, String> params, final boolean isRefresh,
                                         final HttpWebServiceCallBack callBack) {
        SoapReqTask task = new SoapReqTask(businessType, params, isRefresh,
                callBack);
        task.executeOnExecutor(TheradPoolUtil.getInstant().getExecutor());
    }

    /**
     * 请求Http
     *
     * @param @param businessType
     * @param @param params
     * @param @param isRefresh
     * @param @param callBack
     * @return void
     * @throws
     * @Title: getDataHttp
     * @Description: TODO(这里用一句话描述这个方法的作用)
     * @history 1.YYYY-MM-DD author: description:
     */
    private static void getDataHttp(final int businessType,
                                    final Map<String, String> params, final boolean isRefresh,
                                    final HttpWebServiceCallBack callBack) {
        MyApplication.http.get("", new AjaxParams() {

        }, new AjaxCallBack<String>() {
            @Override
            public void onSuccess(String t) {
                // TODO Auto-generated method stub
                super.onSuccess(t);

                NetConnection mNetConnection = NetConnection.getInstant();
                AsResult mAsResult = mNetConnection.parsJosnData(businessType,
                        t);
                if (callBack != null)
                    callBack.onServerDataAcquired(businessType, mAsResult,
                            isRefresh);
            }

            @Override
            public void onFailure(Throwable t, String strMsg) {
                // TODO Auto-generated method stub
                super.onFailure(t, strMsg);
                AsResult mAsResult = new AsResult();
                mAsResult.setResultCode(NetConstant.MSG_CONNECT_JSON_FAILED);
                if (callBack != null)
                    callBack.onServerDataAcquired(businessType, mAsResult,
                            isRefresh);
            }
        });
    }
}
